using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HCManage.Api.Models;
using HCManage.Api.Models.Fileter;
using HCManage.IBll;
using HCManage.Model;
using Microsoft.AspNetCore.Mvc;

namespace HCManage.Api.Controllers.BaseInfo
{
    [Route("api/[controller]")]
    [ApiController]
    public class SysInfoController : Controller
    {
        #region 模块
        private readonly ITblSysInfoBll sysInfoBll;
        private DataResultModel dataResult = new DataResultModel();
        #endregion

        #region 构造函数
        public SysInfoController(ITblSysInfoBll sysInfo_bll)
        {
            this.sysInfoBll = sysInfo_bll;
        }
        #endregion

        /// <summary>
        /// 根据sys_no系統編號查询列表的单个信息
        /// </summary>
        /// <param name="sys_no">键值</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ResultExceptionFilter]
        public JsonResult Get(string id)
        {
            tbl_sysinfo model = sysInfoBll.Get(id);
            dataResult.success = true;
            dataResult.data = model;
            return new JsonResult(dataResult);
        }

        /// <summary>
        /// 查詢當前列表的所有信息
        /// </summary>
        /// <param name="sys_name">系統名稱</param>
        /// <param name="page">页码</param>
        /// <param name="page_size">每页显示的条数</param>
        /// <returns></returns>
        [HttpGet]
        [ResultExceptionFilter]
        public JsonResult List(string sys_name, int? page = 1, int? page_size = 30)
        {
            Tuple<List<tbl_sysinfo>, int> result = sysInfoBll.List(sys_name, page.Value, page_size.Value);
            dataResult.success = true;
            dataResult.data = result.Item1.Select(s => new
            {
                s.sys_no,
                s.sys_name,
                s.sys_charger,
                s.sys_maintenancer,
                s.prd_maintenancer,
                s.test_url,
                s.prd_url,
                s.sys_orderby,
                s.creater,
                s.create_date,
                s.editer,
                s.edit_date
            });
            dataResult.total = result.Item2;
            return new JsonResult(dataResult);
        }

        /// <summary>
        /// 添加当前列表数据
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        public JsonResult Post(tbl_sysinfo model)
        {
            if (model == null)
            {
                throw new Exception("请传入参数");
            }
            bool success = sysInfoBll.Insert(model);
            if (!success)
            {
                throw new Exception("新增失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }

        /// <summary>
        /// 更新当前列表数据
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
         [HttpPut]
        [ResultExceptionFilter]
        public JsonResult Put(tbl_sysinfo model)
        {
            if (model==null)
            {
                throw new Exception("请传入参数");
            }
            bool success = sysInfoBll.Update(model);
            if (!success)
            {
                throw new Exception("新增失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }

        /// <summary>
        ///  删除当前列表数据
        /// </summary>
        /// <param name="sys_no">键值</param>
        /// <returns></returns>
        [HttpDelete]
        [ResultExceptionFilter]
        public JsonResult Delete(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new Exception("请传入参数");
            }
            bool success = sysInfoBll.Delete(id);
            if (!success)
            {
                throw new Exception("新增失败");
            }
            dataResult.success = true;
            return new JsonResult(dataResult);
        }
    }
}
